<?php
/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-09-20
 * 版    本：1.0.0
 * 功能说明：文章控制器。
 *
 **/

namespace Qwadmin\Controller;

use Vendor\Tree;
use Think\Model;
header("Content-type: text/html; charset=utf-8");
class ExtendnovelController extends ComController
{
	public function Index(){
		$Extend = M("extend");
		$list = $Extend->select();
		$this->assign("list",$list);
		$this->display();
	}

	public function Add(){
		if(!$_POST){
			//查出所有平台
			$platform = M("platform")->select();
			$this->assign("platform",$platform);
			$this->display('form');
		}
		$platform_name = $_POST['platform_name'];
		//查询该平台是否已有设置，有的话提示用户执行修改
		$isset = M("extend")->where("platform_name = '$platform_name'")->find();
		if (empty($isset)) {
			$novel_name = $_POST['novel_name'];
			$platform = M("platform")->where("platform_name = '$platform_name'")->find();
			$open_id_pre = $platform['open_id_pre'];
			$domain = $platform['domain'];
			$novel = M("novel")->where("name = '$novel_name'")->find();
			$novel_id = $novel['id'];
			$chapter_id = $_POST['chapter_id'];
			$chapter_info = M("novel_chapter")->where("id = '$chapter_id'")->find();
			$next_chapter_orderset = $chapter_info['order_set']+1;
			$next_chapter_info = M("novel_chapter")->where("novel_id = '$novel_id' and order_set = '$next_chapter_orderset'")->find();
			$next_chapter_id = $next_chapter_info['id'];
			$chapter = $chapter_info['chapter'];
			$title = $chapter_info['title'];
			$is_force = $_POST['is_force'];
			$data = [
				'platform_name'=>$platform_name,
				'open_id_pre'=>$open_id_pre,
				'novel_id'=>$novel_id,
				'novel_name'=>$novel_name,
				'chapter_id'=>$next_chapter_id,
				'domain'=>$domain,
				'chapter'=>$chapter,
				'is_force'=>$is_force,
				'title'=>$title,
				'time'=>time()
			];
			if(M("extend")->add($data)){
				echo json_encode(['status'=>1,'message'=>'设置成功']);
			}else{
				echo json_encode(['status'=>0,'message'=>'设置失败']);
			}
		}else{
				echo json_encode(['status'=>2,'message'=>'该平台已有相关设置，请执行修改操作']);
		}
	}

	public function Getlist(){
		$keyword = $_POST['keyword'];
		$list = M("novel_chapter")->field('id,title,chapter')->where("novel_name = '$keyword'")->limit(20)->select();
		if (!empty($list)) {
			$res_arr = [
				'status'=>1,
				'data'=>$list
			];
		}else{
			$res_arr = [
				'status'=>0,
			];
		}
		echo json_encode($res_arr,JSON_UNESCAPED_UNICODE);
	}

	public function Edit(){
		if(!$_POST){
			$id = $_GET['id'];
//			dump($id);die;
			$extend = M("extend")->where("id = $id")->find();
			$novel_id = $extend['novel_id'];
			$list = M("novel_chapter")->field('id,title,chapter')->where("novel_id = '$novel_id'")->limit(20)->select();
			$chapter_id = $extend['chapter_id'];
			/*
			 * 数据库中chapter_id保存的是推广小说下一章的chapter_id
			 * 但回显应是设置的推广小说当前章的chapter_id
			 */
			$pre_chapter = M("novel_chapter")->field('order_set')->where("id = '$chapter_id'")->find();
			$pre_orderset = $pre_chapter['order_set']-1;
			$pre_chapter_id = M("novel_chapter")->field('id')->where("novel_id = '$novel_id' and order_set = '$pre_orderset'")->find();
			$this->assign('extend',$extend);
			$this->assign('list',$list);
			$this->assign('pre_chapter_id',$pre_chapter_id['id']);
			$this->display('edit');
		}else{
			$id = $_POST['id'];
			$novel_name = $_POST['novel_name'];
			$novel = M("novel")->where("name = '$novel_name'")->find();
			$novel_id = $novel['id'];

			$chapter_id = $_POST['chapter_id'];
			$chapter_info = M("novel_chapter")->where("id = '$chapter_id'")->find();
			$next_chapter_orderset = $chapter_info['order_set']+1;
			$next_chapter_info = M("novel_chapter")->where("novel_id = '$novel_id' and order_set = '$next_chapter_orderset'")->find();
			$next_chapter_id = $next_chapter_info['id'];
			$chapter = $chapter_info['chapter'];
			$title = $chapter_info['title'];
			$is_force = $_POST['is_force'];
			$data = [
				'novel_id'=>$novel_id,
				'novel_name'=>$novel_name,
				'chapter_id'=>$next_chapter_id,
				'chapter'=>$chapter,
				'is_force'=>$is_force,
				'title'=>$title,
				'time'=>time()
			];
			if (M("extend")->where("id = $id")->save($data) === false) {
				echo json_encode(['status'=>0,'message'=>'设置失败']);
			}else{
				echo json_encode(['status'=>1,'message'=>'设置成功']);
			}
		}
	}

	public function Del(){
		$id = $_GET['id'];
		if (empty($id)) {
			echo json_encode(['status'=>2,'message'=>'参数不合法']);
		}else{
			if (M("extend")->where("id = $id")->delete()) {
				$this->success('删除成功','/Extendnovel/index');
			}else{
				$this->error('删除失败','/Extendnovel/index');
			}
		}
	}

}